package com.android.juvid.testnet;

import android.app.ListActivity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import android.util.Log;
import android.widget.ListView;
import android.view.View;
import android.content.DialogInterface;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Button;
import android.view.ViewGroup;
import android.app.AlertDialog;
import android.view.LayoutInflater;
import android.content.Context;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;

public class MyListView3 extends ListActivity {
    private List<Map<String, Object>> mData;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        mData = getData();
//        SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.vlist,
//                new String[]{"title","info","img"},
//                new int[]{R.id.title,R.id.info,R.id.img});
//        setListAdapter(adapter);
        mData = getData();
        MyAdapter adapter = new MyAdapter(this);
        setListAdapter(adapter);

    }


    private List<Map<String, Object>> getData() {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("title", "Juvid");
        map.put("info", "朱古力");
        map.put("img", R.mipmap.i1);
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("title", "Jute");
        map.put("info", "朱特伟");
        map.put("img", R.mipmap.i2);
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("title", "Jutewei");
        map.put("info", "朱伟德");
        map.put("img", R.mipmap.i3);
        list.add(map);
        return list;
    }

    protected void onListItemClick(ListView l, View v, int position, long id) {
        Log.v("listView监听 ", (String)mData.get(position).get("title"));
    }

    /**
     * listview中点击按键弹出对话框
     */
    public void showInfo(){
        new AlertDialog.Builder(this)
                .setTitle("我的第一个列表弹框")
                .setMessage("第一个Android程序,精通iOS,Android,javascript")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                       NextVC();
                    }
                })
                .show();

    }

    public  void NextVC(){
        Intent intent = new Intent();
        intent.setClass(this, MapHash.class);
        startActivity(intent);

    }

    public final class ViewHolder{
        public ImageView img;
        public TextView title;
        public TextView info;
        public Button viewBtn;
    }


    public class MyAdapter extends BaseAdapter{

        private LayoutInflater mInflater;


        public MyAdapter(Context context){
            this.mInflater = LayoutInflater.from(context);
        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mData.size();
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder holder = null;
            if (convertView == null) {

                holder=new ViewHolder();

                convertView = mInflater.inflate(R.layout.vlist2, null);
                holder.img = (ImageView)convertView.findViewById(R.id.img);
                holder.title = (TextView)convertView.findViewById(R.id.title);
                holder.info = (TextView)convertView.findViewById(R.id.info);
                holder.viewBtn = (Button)convertView.findViewById(R.id.view_btn);
                convertView.setTag(holder);

            }else {

                holder = (ViewHolder)convertView.getTag();
            }


            holder.img.setBackgroundResource((Integer)mData.get(position).get("img"));
            holder.title.setText((String)mData.get(position).get("title"));
            holder.info.setText((String)mData.get(position).get("info"));

            holder.viewBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showInfo();
                }
            });
            return convertView;
        }

    }
}
